<#assign base=request.contextPath />
<#import "/tags/select.ftl" as k />
<!DOCTYPE html>
<html>
<head>
  <base href="${base}">	
  <meta charset="utf-8">
  <title>教师评卷</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${base}/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${base}/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="${base}/css/my.css" media="all">
</head>

<body>
	<div class="layui-fluid">
		<div class="layui-card">
			<!-- 搜索栏 -->
			<div id="search-form" class="layui-form layui-card-header layuiadmin-card-header-auto">
				<div class="layui-form-pane">
					<div class="layui-form-item">
						<label class="layui-form-label" style="width: 60px;">学号</label>
						<div class="layui-input-inline" style="width: 180px;">
							<input type="text" name="Q_LIKES_student.username" class="layui-input" placeholder="学号" />
						</div>
						<label class="layui-form-label" style="width: 60px;">姓名</label>
						<div class="layui-input-inline" style="width: 180px;">
							<input type="text" name="Q_LIKES_student.fullname" class="layui-input" placeholder="姓名" />
						</div>
						<div class="layui-input-inline">
							<div class="layui-btn-group">
								<button class="layui-btn" lay-submit lay-filter="LAY-search">
	            					<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
	          					</button>
								<button class="layui-btn layui-btn-warm" id="LAY-reset" lay-filter="LAY-reset">
	            					<i class="layui-icon layui-icon-close layuiadmin-button-btn"></i>清除
	          					</button>
							</div>
						</div>
					</div>
				</div>
			</div>			
			
			<div class="layui-row">
	            <div class="layui-col-md2">
	                <!-- 组织树 -->
	                <ul id="orgTree"></ul>
	            </div>
	            <div class="layui-col-md10">		
					<!-- 表格 -->
					<div class="layui-card-body">
						<div style="padding-bottom:10px">
							<div class="layui-btn-group">
								<button class="layui-btn layui-btn-primary layuiadmin-btn-list" data-type="expxls">导出选中</button>
								<button class="layui-btn layui-btn-primary layuiadmin-btn-list" data-type="expxlsAll">导出全部</button>
							</div>
							<span id="parentName" style="color:red;margin-left:20px;font-weight:bold;">全部</span>
						</div>
						<table id="LAY-table" lay-filter="LAY-table"></table>
					</div>
				</div>
			</div>
			
		</div>
	</div>

	<!-- 自定义模板 -->
  	<script type="text/html" id="tr-opts">
    	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pigai"><i class="layui-icon layui-icon-edit"></i>批改试卷</a>
  	</script>
  	
  	<script src="${base}/js/lib/jquery-1.11.3.min.js"></script>
  	<script src="${base}/js/commons.js"></script>
  	<script src="${base}/layuiadmin/layui/layui.js"></script>
  	<script>
  	layui.config({
    	base: '${base}/layuiadmin/' //静态资源所在路径
  	}).extend({
    	index: 'lib/index'
  	}).use(['index', 'table', 'laydate','tree'], function(){
    	var table = layui.table;
    	var form = layui.form;
    	
    	// 同步请求，获得节点数据
        function getTreeNodes() {
            var nodes = [];
            $$.ajax({
            	url: '${base}/system/dept/treeAll',
                async: false,
                success: function (data) {
                    nodes = data;
                }
            });
            return nodes;
        }
    	
     	// 同步请求，获得节点数据
    	var nodes = getTreeNodes();
    	window.parentId = 0; //选中    	
    	// 渲染组织树
    	var tree = layui.tree({
    		elem: '#orgTree',
    		nodes: nodes,
    		click: function(node) {
    			var html = "父机构：" + node.name;
    			if(node.children) html += "（"+node.children.length+"）";
    			$('#parentName').html(html);
    			parentId = node.id;
    			table.reload('LAY-table', {
            		where: {
            			'orgId': parentId
            		},
            		page: {curr: 1}
          		});
    		}
    	});
    	
    	// 创建表格
    	var tbconfig = $.extend($$.tbconfig, {
			cols: [[
				{checkbox: true},
				{title: '#', type:'numbers', align: 'center', width: 40},
				{title: '状态', field: 'status', width: 100, templet: function(d) {
					if(d.status == 0) return '<span class="my-bg-red">未提交</span>';
					if(d.status == 1) return '<span class="my-bg-blue">已提交</span>';
					if(d.status == 2) return '<span class="my-bg-green">批改完成</span>';
				}},
				{title: '学号',width: 150, javaRender:'student.getUsername', templet: function(d) {
					return d.student.username
				}},  
				{title: '姓名', minWidth: 120, javaRender:'student.getFullname', templet: function(d) {
					return d.student.fullname
				}},  
				{title: '班级', width: 150, exportXls:false, templet: function(d) {
					return d.student.dept.depName
				}},  
				{title: '成绩', field: 'score', width: 120},
				{title: '操作', fixed: 'right', width: 130, align: 'center', toolbar: '#tr-opts'}
			]], 
			url: '${base}/exam/examresult/list'
		});
        table.render(tbconfig);
        
     	// 排序
        table.on('sort(LAY-table)', function(obj) {
        	table.reload('LAY-table', {
        		initSort: obj,
        		where: obj
        	})
        });
    	//监听搜索
    	form.on('submit(LAY-search)', function(data) {
      		var field = data.field;      
      		//执行重载
      		table.reload('LAY-table', {
        		where: field,
        		page: {curr: 1}
      		});
    	});
    	//重置
    	$('#LAY-reset').click(function() {
    		$('#search-form :input').val('');
    	});
    	
     	// 工具栏
        table.on('tool(LAY-table)', function(obj) {
            var data = obj.data;
            if(obj.event === 'pigai') {
            	var idx = layer.open({
	          		type: 2,
	          		title: '批改试卷 - 【'+data.student.fullname+'】',
	          		content: '${base}/exam/student/scoreExam/' + data.oid,
	          		maxmin: true,
	          		btn: ['确定', '取消'],
	          		yes: function(index, layero){
	            		//点击确认触发 iframe 内容中的按钮提交
	            		var submit = layero.find('iframe').contents().find("#LAY-form-submit");
	            		submit.click();
	          		}
	        	});
            	layer.full(idx);
            }
        });
    	
    	//获得导出字段
    	getExportCols = function(tbconfig) {
    		var colIds = [];
    		var colNames = [];
    		var cols = tbconfig.cols[0];
    		for(var i=0; i<cols.length; i++) {
    			if(cols[i].exportXls===false) continue;
    			// 如果存在field字段
    			if(cols[i].javaRender) {
    				colIds.push("javaRender" + cols[i].javaRender);
    				colNames.push(cols[i].title);
    			} else if(cols[i].ref) {
    				colIds.push(cols[i].ref);
    				colNames.push(cols[i].title);
    			} else if(cols[i].field) {
    				colIds.push(cols[i].field);
    				colNames.push(cols[i].title);
    			}
    		}
    		return {
    			colIds: colIds,
    			colNames: colNames
    		};
    	}
    	
    	// toolbar工具栏
    	var active = {
      		batchdel: function() {
        		var checkData = table.checkStatus('LAY-table').data; //得到选中的数据
        		if(checkData.length === 0) {
        			layer.msg('请选择数据');
          			return false;
        		}
        			
        		var idArr = [];
        		for(var i=0; i<checkData.length; i++) {
        			idArr.push(checkData[i].oid);
        		}
        		layer.confirm('确定删除吗？', function(index) {
        			$$.ajax({
        				url: '${base}/exam/examresult/multiDel',
        				data: {
        					ids: idArr.join(',')
        				},
        				success: function(json) {
        					table.reload('LAY-table');
                  			layer.msg('已删除');	
        				}        				
        			});          			
        		});
      		},
	      	add: function() {
	        	layer.open({
	          		type: 2,
	          		title: '增加',
	          		content: '${base}/exam/examresult/html/add',
	          		maxmin: true,
	          		area: ['750px', '500px'],
	          		btn: ['确定', '取消'],
	          		yes: function(index, layero){
	            		//点击确认触发 iframe 内容中的按钮提交
	            		var submit = layero.find('iframe').contents().find("#LAY-form-submit");
	            		submit.click();
	          		}
	        	}); 
      		},
      		// 导出excel，选中
      		expxls: function() {
    			var checkData = table.checkStatus('LAY-table').data; //得到选中的数据
	    		if(checkData.length === 0) {
	    			layer.msg('请至少选择一条数据！');
	      			return false;
	    		}	    			
	    		var idArr = [];
	    		for(var i=0; i<checkData.length; i++) {
	    			idArr.push(checkData[i].oid);
	    		}
      			// ajax下载
      			var cols = getExportCols(tbconfig);
      			var data = {
   					ids: idArr.join(','),
   					colIds: cols.colIds.join(','),
   					colNames: cols.colNames.join(',')
   				};
      			ajax_download('${base}/exam/examresult/html/exportSel', data, $);
      		},
      		// 导出全部
      		expxlsAll: function() {
      			// ajax下载
      			var cols = getExportCols(tbconfig);
      			var data = {
	      			// 带上查询条件
   					colIds: cols.colIds.join(','),
   					colNames: cols.colNames.join(',')
   				};
      			ajax_download('${base}/exam/examresult/html/exportAll', data, $);
      		}
   		}; 

		$('.layui-btn.layuiadmin-btn-list').on('click', function(){
      		var type = $(this).data('type');
      		active[type] ? active[type].call(this) : '';
		});

  	});
  </script>
</body>
</html>
